import { Component, OnInit, Input, EventEmitter } from '@angular/core';
import { LoginService } from '../../services/login.service';
import {
  Router,
  ActivatedRoute,
  NavigationEnd } from '@angular/router';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.scss']
})
export class MenuComponent implements OnInit {
  @Input() formartMenu;

  isCollapsed = false;

  recipesMenu: Array<object>;

  user: any;

  constructor(
    private loginService: LoginService,
  ) { }

  ngOnInit() {
    this.user = this.loginService.getUser();
    // menu data
    this.recipesMenu = [
      {
        name: '菜谱',
        isActive: true,
        href: '',
        children: [
          {
            name: '食谱管理',
            auth: 'recipes/list/1',
            href: 'recipes/recipes-manage',
            selected: false
          }, {
            name: '渠道管理',
            auth: 'channel/list/1',
            href: 'recipes/channel-manage',
            selected: false
          }, {
            name: '标签管理',
            auth: 'tag/list/1',
            href: 'recipes/label-manage',
            selected: false
          }, {
            name: '原料管理',
            auth: 'material/list/1',
            href: 'recipes/material-manage',
            selected: false
          }
        ]
      }
    ];

    // auth
    // this.user['authList']
    if (this.user) {
      this.recipesMenu.forEach(item => {
        if (item['children']) {
          item['children'].forEach(ite => {
            let bool = false;
            this.user['authList'].forEach(it => {
              if (ite['auth'] === it) {
                bool = true;
              }
            });
            if (bool) {
              ite['display'] = true;
            }
          });
        }
      });
    }

    this.formartMenu.subscribe(res => {
      // console.log('接收到信号了');
      this.formartMenuList();
    });

  }

  formartMenuList() {
    this.recipesMenu[0]['children'].forEach((item) => {
      item.selected = false;
    });
  }

  toggleCollapsed(): void {
    this.isCollapsed = !this.isCollapsed;
  }

  toggleMenu(item): void {
    // this.recipesMenu.forEach( v => { v['isActive'] = false; });
    // item.isActive = !item.isActive;
    this.formartMenuList();
    item.selected = true;
  }

}
